package com.kungfuhacking.wristbandpro.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.utils.DensityUtil;


/**
 * 自定义带删除按钮的edittext
 * Created by Aaron on 17/2/22.
 */

public class InputView extends RelativeLayout {

    private String TAG = getClass().getSimpleName();

    private ImageView iv_delete;
    private EditText et_text;

    public InputView(Context context) {
        this(context, null);
    }

    public InputView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {

        //加载自定义控件布局
        LayoutInflater.from(context).inflate(R.layout.custom_edittext, this, true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputView);

        ImageView imageView = (ImageView) findViewById(R.id.iv_left_view);
        et_text = (EditText) findViewById(R.id.et_text);
        iv_delete = (ImageView) findViewById(R.id.iv_delete);

        if (typedArray == null)
            return;

        int backgroudResource = typedArray.getResourceId(R.styleable.InputView_backgroundResc, -1);
        if (backgroudResource != -1)
            setBackgroundResource(backgroudResource);

        if (typedArray.getBoolean(R.styleable.InputView_showLeftImage, false)) {
            imageView.setVisibility(VISIBLE);
            int resourceId = typedArray.getResourceId(R.styleable.InputView_leftImage, -1);

            if (resourceId != -1)
                imageView.setImageResource(resourceId);

        } else
            imageView.setVisibility(GONE);

        et_text.setHint(typedArray.getString(R.styleable.InputView_textHint));

        et_text.setHintTextColor(typedArray.getColor(R.styleable.InputView_textHintColor, getResources().getColor(R.color.bbb)));
        et_text.setTextColor(typedArray.getColor(R.styleable.InputView_textColor, getResources().getColor(R.color.bbb)));

        //获得的是像素, 所以要除以density
        et_text.setTextSize(typedArray.getDimensionPixelSize(R.styleable.InputView_textSize, DensityUtil.dip2px(getContext(), 15)) / getResources().getDisplayMetrics().density);

        et_text.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                if (hasFocus) {

                    iv_delete.setVisibility(et_text.getText().toString().length() > 0 ? VISIBLE : INVISIBLE);

                    et_text.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                            iv_delete.setVisibility(s.length() > 0 ? VISIBLE : INVISIBLE);
                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                        }
                    });

                } else
                    iv_delete.setVisibility(INVISIBLE);

            }
        });

        iv_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_text.setText("");
                et_text.requestFocus();
            }
        });


        typedArray.recycle();
    }


    public void setInputType(int type) {
        et_text.setInputType(type);
    }


    public String getText() {
        return et_text.getText().toString();
    }

    public void setTxt(String txt) {
        et_text.setText(txt);
        et_text.setSelection(txt.length());
    }

}
